package com.yswy.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author: bing
 * @date 2022-02-23 18:21:52
 * @description: 阿里云短信
 */
@Component
//@PropertySource(value="classpath:application.properties",encoding = "utf-8")
public class AliSmsUtils {
    @Value("${ali.sms.accessKeyId}")
    private String accessKeyId;
    @Value("${ali.sms.secretKey}")
    private String secretKey;
    @Value("${ali.sms.smsSign}")
    private String smsSign;
    @Value("${ali.sms.bind.template}")
    private String template;

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * @param
     * @return
     * @author bing
     * @Description 发送验证码
     * @date 2022/2/23 18:26
     */
    public void send(String phone, String code) {
        com.aliyun.dysmsapi20170525.Client client = null;
        try {
            client = AliSmsUtils.createClient(accessKeyId, secretKey);

            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName(smsSign)
                    .setTemplateCode(template)
                    .setTemplateParam("{\"code\":\"" + code + "\"}");
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse s = client.sendSms(sendSmsRequest);
            System.out.println(JSONObject.toJSONString(s));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static String getRandomPwd(int len) {
        String result = null;
        while(len==32){
            result = makeRandomPwd(len);
            if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*")) {
                return result;
            }
            result = makeRandomPwd(len);
        }
        return "长度不得少于32位!";
    }

    /**
     * @Title: makeRandomPwd
     * @Description:随机密码生成
     * @param len
     * @return String
     * @throws
     */
    public static String makeRandomPwd(int len) {
        char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int x = 0; x < len; ++x) {
            sb.append(charr[r.nextInt(charr.length)]);
        }
        return sb.toString();
    }


//    public static void main(String[] args) {
//        send("18080455214" , );
//    }
}
